function AllReadyList() {
    console.log("11");
    // Ext.getCmp("viewPort").items.items[0].insert(24, {
    //     itemId: "demb",
    //     text: "保存调整时间",
    //     width: this.itemWidth,
    //     iconCls: `iconfont iconsave`,
    // }); //指定位置插入按钮
    setTimeout(function () {
        const mList = Ext.getCmp('KcBillheadList');
        const mList1 = Ext.getCmp('KcBillbodyList');

        const toolbar = Ext.getCmp("ext-comp-1036")

        toolbar.insert(24, {
            itemId: "demb-t",
            text: "修改验收时间",
            width: this.itemWidth,
            iconCls: "iconfont iconsave" //按钮形状
        });

        toolbar.insert(24, {
            itemId: "demb",
            text: "安调确认时间",
            width: this.itemWidth,
            iconCls: "iconfont iconsave" //按钮形状
        });
        toolbar.get('demb').on('click', function () {
            const selected = mList.getSelectionModel().getSelection();
            const selected1 = mList1.getSelectionModel().getSelection();
            console.log(selected, 'selected')
            console.log(selected1, 'selected')
            if (selected1.length > 0) {
                // const PhId = selected1[0].data.PhId

                try {
                    createWindow(OkBtn1)?.show()
                } catch (error) {
                    Ext.Msg.alert("提示", "修改安调时间出现错误请联系服务器客服");
                }
            } else {
                Ext.Msg.alert("提示", "请选择对应明细表再次点击修改安调时间");
            }
        })
        toolbar.get('demb-t').on('click', function () {
            const selected = mList.getSelectionModel().getSelection();
            const selected1 = mList1.getSelectionModel().getSelection();
            console.log(selected, 'selected')
            console.log(selected1, 'selected')
            if (selected1.length > 0) {
                // const PhId = selected1[0].data.PhId

                try {
                    createWindow(OkBtn2)?.show()
                } catch (error) {
                    Ext.Msg.alert("提示", "修改验收时间出现错误请联系服务器客服");
                }
            } else {
                Ext.Msg.alert("提示", "请选择对应明细表再次点击修改验收时间");
            }
        })
    }, 200);


}

    //确认
    function OkBtn1(datePicker,window){
        const selectedDate = datePicker.getValue();
        const formattedDate = Ext.Date.format(selectedDate, 'Y-m-d');
        const dgrid = Ext.getCmp('KcBillbodyList');
        const currentrows = dgrid.getSelectionModel().getSelection()
        console.log(currentrows, 'currentrows')
        if (currentrows.length > 0) {
            currentrows.forEach(record => {
                console.log(record, 'record');
                if (record.data.PhId && formattedDate) {
                    console.log(record.data.PhId, 'record.data.PhId');
                    console.log(record.data.PhId.length, 'record.data.PhId');
                    if (record.data.PhId.length == 16) {
                        execServer('updateTzsj', { phid: record.data.PhId, date: formattedDate }, function (res) {
                            console.log(res.status == "success", "updateTzsj")
                            if (res.status == "success") {
                                Ext.Msg.alert("提示", "状态更新成功，请刷新页面查看");
                            } else {
                                Ext.Msg.alert("提示", "状态更失败，请刷新页面查看");
                            }
                        })
                    } else {
                        Ext.Msg.alert("提示", "状态更失败，Phi不满足16位更新条件");
                    }
                }
            })
        } else {
            // Ext.Msg.alert("提示", "请选择对应明细表再次点击保存安调时间");
            // currentrows[0].set("u_tcrq", formattedDate)
        }
        // toolbar.get("save").enable()
        // 关闭弹窗
        window.close();
    }
    function OkBtn2(datePicker,window){
        const selectedDate = datePicker.getValue();
        const formattedDate = Ext.Date.format(selectedDate, 'Y-m-d');
        const dgrid = Ext.getCmp('KcBillbodyList');
        const currentrows = dgrid.getSelectionModel().getSelection()
        console.log(currentrows, 'currentrows')
        if (currentrows.length > 0) {//kc_billbody
            currentrows.forEach(record => {
                console.log(record, 'record');
                if (record.data.PhId && formattedDate) {
                    console.log(record.data.PhId, 'record.data.PhId');
                    console.log(record.data.PhId.length, 'record.data.PhId');
                    if (record.data.PhId.length == 16) {
                        execServer('updateYsDate', { phid: record.data.PhId, date: formattedDate }, function (res) {
                            console.log(res.status == "success", "updateTzsj")
                            if (res.status == "success") {
                                Ext.Msg.alert("提示", "状态更新成功，请刷新页面查看");
                            } else {
                                Ext.Msg.alert("提示", "状态更失败，请刷新页面查看");
                            }
                        })
                    } else {
                        Ext.Msg.alert("提示", "状态更失败，Phi不满足16位更新条件");
                    }
                }
            })
        } else {
            // Ext.Msg.alert("提示", "请选择对应明细表再次点击保存安调时间");
            // currentrows[0].set("u_tcrq", formattedDate)
        }
        // toolbar.get("save").enable()
        // 关闭弹窗
        window.close();
    }
function createWindow(fun) {
    // 创建提示标签
    const label = Ext.create('Ext.form.Label', {
        text: '请选择要重置的时间',
        style: 'margin-bottom: 10px;'
    });

    // 创建日期选择器
    const datePicker = Ext.create('Ext.picker.Date', {
        format: 'Y-m-d', // 设置日期格式
        width: 220
    });

    // 创建确认按钮
    const okButton = Ext.create('Ext.button.Button', {
        text: '确定',
        handler: ()=>{fun(datePicker,window)}
    });

    // 创建取消按钮
    const cancelButton = Ext.create('Ext.button.Button', {
        text: '取消',
        handler: function () {
            // 关闭弹窗
            window.close();
        }
    });

    // 创建弹窗
    const window = Ext.create('Ext.window.Window', {
        title: '请选择日期',
        width: 300,
        height: 315,
        layout: 'fit', // 使用 vbox 布局，以便垂直排列组件
        items: [datePicker],
        buttons: [okButton, cancelButton],
        modal: true // 设置为模态窗口
    });
    return window;
}
function AllReady() {
    var mstform = Ext.getCmp("KC_INKCGRK_Head"); //获取容器
    var dgrid = Ext.getCmp("KC_INKCGRK_Body");
    var Toolbar = Ext.getCmp("toolbar");
    mstform.on("dataready", function () {
        console.log("Data ready event triggered");
        if (otype == "view") {
            console.log("view");
            Toolbar.get("save").disabled = false;
            dgrid.setReadOnlyCol("user_atdt", false);
        }
    });

    var dstore = dgrid.store;

    var PhidTrProj = mstform.queryById("PhidTrProj"); //项目工程
    var user_pc_no = mstform.queryById("user_pc_no")//项目编号
    var user_division = mstform.queryById("user_division"); //获取字段
    var user_product_line = mstform.queryById("user_product_line");

    var user_yhd_name = mstform.queryById("user_yhd_name");
    var user_yhd_no = mstform.queryById("user_yhd_no");
    var user_yhd_dt = mstform.queryById("user_yhd_dt");
    var user_yhd_amt = mstform.queryById("user_yhd_amt"); //要货单金额--不含税
    var PhidContractno = mstform.queryById("PhidContractno"); //合同编码
    var ContractName = mstform.queryById("ContractName");
    var user_cnt_amt = mstform.queryById("user_cnt_amt");
    var PhidCompno = mstform.queryById("PhidCompno");
    var user_regional_center = mstform.queryById("user_regional_center");
    var user_sl_organize = mstform.queryById("user_sl_organize"); //收料组织
    var PhidDeptno = mstform.queryById("PhidDeptno"); //收料部门
    var user_ck_no = mstform.queryById("user_ck_no");
    var PhidChkpsn = mstform.queryById("PhidChkpsn");
    var PhidWarehouse = mstform.queryById("PhidWarehouse"); //新增时根据方案自动生成不同的汇总层级

    // var phid_schemeid = mstform.getItem("phid_schemeid"); //方案ID

    // if (otype == "add") {
    // 	console.log(phid_schemeid.getValue(), "phid_schemeid.getValue()");

    // 	// if (phid_schemeid.getValue() == "747220909017002") {
    // 	// 	u_cj.setValue("2");
    // 	// 	BatchBindCombox([u_cj]);
    // 	// } else if (phid_schemeid.getValue() == "747220909023003") {
    // 	// 	u_cj.setValue("1");
    // 	// 	BatchBindCombox([u_cj]);
    // 	// }
    // }

    PhidTrProj.addListener("helpselected", function () {
        execServer(
            "getPtInfoByPhid",
            { phid: PhidTrProj.getValue() },
            function (res) {
                const obj = JSON.parse(res.data)[0].extendObjects;
                console.log(obj, "obj");
                user_division.setValue(obj.org_phid);
                user_product_line.setValue(obj.midwife_phid);
                user_regional_center.setValue(obj.regional_center_phid);
                user_pc_no.setValue(obj.pc_no)
                BatchBindCombox([user_division]);
                BatchBindCombox([user_product_line]);
                BatchBindCombox([user_regional_center]);
            }
        );
    });
    PhidContractno.addListener("change", function () {
        console.log(PhidContractno.getValue(), "PhidContractno.getValue()");
        execServer(
            "getCntOrg",
            { phid: PhidContractno.getValue() },
            function (res) {
                if (res.count > 0) {
                    try {
                        var obj = JSON.parse(res.data)[0].extendObjects;
                        console.log(obj, "obj");
                        user_sl_organize.setValue(obj.phid_org);
                        PhidDeptno.setValue(obj.user_apply_dept);
                        BatchBindCombox([user_sl_organize]);
                        BatchBindCombox([PhidDeptno]);
                    } catch (error) { }
                }
            }
        );

        // 获取合同信息
        getCntMInfoByPhid(mstform.queryById("PhidContractno").getValue())
    });
    PhidContractno.on("beforetriggerclick", function (eOp, ignoreBeforeEvent) {
        //选择合同之前先选择项目名称
        if (PhidTrProj.getValue()) {
            // user_yhd_name.setOutFilter({ phid_pc: PhidTrProj.getValue() });
        } else {
            Ext.Msg.alert("提示", "请先选择项目工程");
            mstform.getItem("PhidTrProj").focus();
            return false;
        }
    });
    //要货单根据合同过滤

    user_yhd_name.on("beforetriggerclick", function (eOp, ignoreBeforeEvent) {
        console.log(PhidContractno.getValue());

        if (PhidContractno.getValue()) {
            user_yhd_name.setOutFilter({ phid_contract: PhidContractno.getValue() });
        } else {
            Ext.Msg.alert("提示", "请先选择合同");
            mstform.getItem("PhidContractno").focus();
            return false;
        }
    });
    user_yhd_name.addListener("helpselected", function () {
        execServer(
            "getOrderInfo",
            { yhd: user_yhd_name.getValue() },
            function (res) {
                if (res.count > 0) {
                    var obj = JSON.parse(res.data);
                    user_yhd_no.setValue(obj[0].extendObjects.no);
                    user_yhd_dt.setValue(obj[0].extendObjects.dt);
                    user_yhd_amt.setValue(obj[0].extendObjects.yhd_amt);
                    PhidContractno.setValue(obj[0].extendObjects.phid);
                    ContractName.setValue(obj[0].extendObjects.cnt_name);
                    user_cnt_amt.setValue(obj[0].extendObjects.cnt_amt);
                    PhidCompno.setValue(obj[0].extendObjects.gys);
                    BatchBindCombox([PhidContractno]);
                    BatchBindCombox([PhidCompno]);

                    console.log(PhidContractno, "PhidContractno");
                }
            }
        );
    });

    /*var user_yhd_name = mstform.getItem('user_yhd_name');
    user_yhd_name.on('beforetriggerclick', function (eOp, ignoreBeforeEvent) {
user_yhd_name.setOutFilter({ phid_pc: PhidTrProj.getValue() })
});*/

    PhidWarehouse.addListener("helpselected", function () {
        execServer(
            "getStoreInfo",
            { store: PhidWarehouse.getValue() },
            function (res) {
                if (res.count > 0) {
                    var obj = JSON.parse(res.data);
                    user_ck_no.setValue(obj[0].extendObjects.no);
                    PhidChkpsn.setValue(obj[0].extendObjects.manapsn)
                    BatchBindCombox([PhidChkpsn])
                }
            }
        );
    });

    if (otype == "add") {
        dstore.on("datachanged", function (store, records, index, eOpts) {
            SetTotal();
        });
        dstore.on("add", function (store, records, index, eOpts) {
            SetTotal();
        });
        dstore.on("remove", function (store, records, index, eOpts) {
            SetTotal();
        });
    }
    dgrid.addListener("edit", function (editor, e) {
        var record = e.record;
        if (e.originalValue == e.value) {
            return;
        }

        record.set("user_yfd_rest_qty", record.data.Qty);

        // record.set("u_fpje");
        SetTotal();
    });

    function SetTotal() {
        let sum_amt = 0;
        dstore.each(function (item) {
            console.log("shcuhujine" + item.get("TaxMony"));
            sum_amt += item.get("TaxMony");
        });
        mstform.getItem("user_rk_amt").setValue(sum_amt);
    }

    function getCntMInfoByPhid(phid){
        if(!phid) return 
        const user_sup_con_person = mstform.queryById("user_sup_con_person")
        execServer("getCntMInfoByPhid",{phid},(res)=>{
            console.log(`合同信息`,res);
            if(res.count ==0) return
            const _data = JSON.parse(res.data)[0].extendObjects
            user_sup_con_person.setValue(_data.gflxr_name)
        })
    }

    if (otype == $Otype.ADD || otype == $Otype.EDIT || otype == $Otype.COPY) {
        dstore.on("datachanged", async function (dstore, eOpts) {
            const records = dstore.getRange();
            for (const record of records) {
                try {
                    if (!record.data.user_storage_type) {
                        //如果为空，则默认为合格默认
                        record.set("user_storage_type", "RKLX0001");
                    }
                    record.set("user_if_kxys", 0);

                    record.set("user_yfd_rest_qty", record.data.Qty);
                } catch (error) {
                    console.error(error);
                }
            }
        });
    }
}
